package com.ruoyi.web.controller.client;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.client.KeyBindingRecords;
import com.ruoyi.system.service.IKeyBindingRecordsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 关重件绑定记录Controller
 *
 * @author ruoyi
 * @date 2023-02-20
 */
@Api(tags = "关重件绑定记录api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/keyBinding/records" )
public class KeyBindingRecordsController extends BaseController {

    private final IKeyBindingRecordsService keyBindingRecordsService;

    /**
     * 查询关重件绑定记录列表
     */
    @ApiOperation(value = "查询关重件绑定记录列表")
    @GetMapping("/list")
    public TableDataInfo list(KeyBindingRecords keyBindingRecords)
    {
        startPage();
        LambdaQueryWrapper<KeyBindingRecords> lqw = new LambdaQueryWrapper<KeyBindingRecords>();
        if (StringUtils.isNotBlank(keyBindingRecords.getAssemblyCode())){
            lqw.like(KeyBindingRecords::getAssemblyCode ,keyBindingRecords.getAssemblyCode());
        }
        if (StringUtils.isNotBlank(keyBindingRecords.getOrderCode())){
            lqw.like(KeyBindingRecords::getOrderCode ,keyBindingRecords.getOrderCode());
        }
        if (StringUtils.isNotBlank(keyBindingRecords.getSerialNumber())){
            lqw.eq(KeyBindingRecords::getSerialNumber ,keyBindingRecords.getSerialNumber());
        }

        List<KeyBindingRecords> list = keyBindingRecordsService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出关重件绑定记录列表
     */
    @ApiOperation(value = "导出关重件绑定记录列表")
    @Log(title = "关重件绑定记录" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(KeyBindingRecords keyBindingRecords) {
        LambdaQueryWrapper<KeyBindingRecords> lqw = new LambdaQueryWrapper<KeyBindingRecords>(keyBindingRecords);
        List<KeyBindingRecords> list = keyBindingRecordsService.list(lqw);
        ExcelUtil<KeyBindingRecords> util = new ExcelUtil<KeyBindingRecords>(KeyBindingRecords. class);
        return util.exportExcel(list, "关重件绑定记录" );
    }

    /**
     * 获取关重件绑定记录详细信息
     */
    @ApiOperation(value = "获取关重件绑定记录详细信息")
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(keyBindingRecordsService.getById(id));
    }

    /**
     * 新增关重件绑定记录
     */
    @ApiOperation(value = "新增关重件绑定记录")
    @Log(title = "关重件绑定记录" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KeyBindingRecords keyBindingRecords) {
        return toAjax(keyBindingRecordsService.save(keyBindingRecords) ? 1 : 0);
    }

    /**
     * 修改关重件绑定记录
     */
    @ApiOperation(value = "修改关重件绑定记录")
    @Log(title = "关重件绑定记录" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KeyBindingRecords keyBindingRecords) {
        return toAjax(keyBindingRecordsService.updateById(keyBindingRecords) ? 1 : 0);
    }

    /**
     * 删除关重件绑定记录
     */
    @ApiOperation(value = "删除关重件绑定记录")
    @Log(title = "关重件绑定记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(keyBindingRecordsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
